/**
 * 
 */
package tp.pr5.instructions;

import tp.pr5.Constants;
import tp.pr5.NavigationModule;
import tp.pr5.RobotEngine;
import tp.pr5.instructions.exceptions.InstructionExecutionException;
import tp.pr5.instructions.exceptions.WrongInstructionFormatException;
import tp.pr5.items.ItemContainer;

/**
 * Its execution moves the robot from one place to the next one in the current direction. This instruction works if the user writes MOVE or MOVER
 */
public class MoveInstruction implements Instruction {
	private NavigationModule navigation;
	private RobotEngine engine;
	
	/**
	 * It builds a default MoveInstruction
	 */
	public MoveInstruction()
	{}
	

	@Override
	/**
	 * Parses the String returning a MoveInstruction instance or throwing a WrongInstructionFormatException()
	 * @param engine - The robot engine
	 * @param navigation - The information about the game, i.e., the places, current direction and current heading to navigate
 	 * @param robotContainer - The inventory of the robot
	 */
	public Instruction parse(String cad) throws WrongInstructionFormatException {
		if (cad.equalsIgnoreCase("MOVE") || cad.equalsIgnoreCase("MOVER"))
		{
			return new MoveInstruction();
		}
		else
			throw new WrongInstructionFormatException();
	}


	@Override
	/**
	 * Returns a description of the Instruction syntax. The string does not end with the line separator. It is up to the caller adding it before printing.
	 * @return the command syntax MOVE|MOVER
	 */
	public String getHelp() {
		return "MOVE|MOVER";
	}

	/**
	 * Set the execution context. The method receives the entire engine (engine, navigation and the robot container) even though the actual implementation of execute() may not require it.
	 * @param engine - The robot engine
	 * @param navigation - The information about the game, i.e., the places, current direction and current heading to navigate
	 * @param robotContainer - The inventory of the robot
	 */
	@Override
	public void configureContext(RobotEngine engine, NavigationModule navigation, ItemContainer robotContainer) {
		this.engine = engine;
		this.navigation = navigation;
	}

	@Override
	/**
	 * Moves from the current place to the next place on the current Direction. An opened street must exist between both places to be moved
	 * @throws InstructionExecutionException - When the robot cannot go to other place (there is a wall, a closed street...)
	 */
	public void execute() throws InstructionExecutionException {
		try{
			this.navigation.move();
			this.engine.addFuel(-Constants.LOST_FUEL);
		}
		catch(Exception e){
			throw new InstructionExecutionException(e.getMessage());
		}
	}

}
